﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using Frame.Lib.Core.Tree;
using Frame.Lib.Enums;
using Frame.Lib.Core.Base;
using Frame.Lib.Core.User;
using Frame.Lib.Core.Role;
using Frame.Lib.Common;

namespace Frame.Lib.Core.Module
{
    public class ModuleBO : TreeBO<ModuleInfo>
    {
        private ModuleDAL _module;
        public ModuleBO()
            : base(STableName.Module)
        {
            _module = new ModuleDAL();
        }

        protected override BaseDAL DAL
        {
            get
            {
                return _module;
            }
        }

        public object GetIDByCode(ModuleCode code)
        {
            return base.GetIDByCode(code.ToString());
        }

        public List<ModuleInfo> GetList(int upperID, int userID)
        {
            IEnumerable<ModuleInfo> list = base.GetList(upperID);

            UserBO user = new UserBO();
            RoleBO role = new RoleBO();
            IEnumerable<int> moduleIDs = user.GetModuleIDs(userID, role);


            if (userID != Constants.ADMIN_ID)
            {
                list = from module in list
                       where moduleIDs.Contains(module.ID)
                       select module;
            }
            List<ModuleInfo> result = list.ToList();

            SetDelegate(result);
           
            return result;
        }

        /// <summary>
        /// 返回经过权限筛选的模块
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public List<ModuleInfo> GetList(List<int> ids, int userID)
        {
            UserBO user = new UserBO();
            RoleBO role = new RoleBO();
            IEnumerable<int> moduleIDs = user.GetModuleIDs(userID, role);

            IEnumerable<ModuleInfo> list = from data in base.GetList()
                                           where ids.Contains(data.ID)
                                           orderby data.Orders
                                           select data;
            if (userID != Constants.ADMIN_ID)
            {
                list = list.Where(module => moduleIDs.Contains(module.ID));
            }
            List<ModuleInfo> result = list.ToList();

            SetDelegate(result);           

            return result;

        }

        public override List<ModuleInfo> GetList(int upperID)
        {
            List<ModuleInfo>  list = base.GetList(upperID);
            SetDelegate(list);
            return list;
        }

        public System.Data.DataTable GetIcons()
        {
            string strIcoPath = System.Web.HttpContext.Current.Server.MapPath(@"~\common\images\Module");
            DirectoryInfo dirInfo = new DirectoryInfo(strIcoPath);
            FileInfo[] files = dirInfo.GetFiles();
            DataTable dtIcon = new DataTable();
            dtIcon.Columns.Add("url");
            dtIcon.Columns.Add("relativeUrl");
            foreach (FileInfo file in files)
            {
                if (file.Extension == ".gif" || file.Extension == ".jpg")
                {
                    DataRow dr = dtIcon.NewRow();
                    dr["url"] = "~/common/images/module/" + file.Name;
                    dr["relativeUrl"] = "common/images/module/" + file.Name;
                    dtIcon.Rows.Add(dr);
                }
            }
            return dtIcon;
        }

        public override void Save(ModuleInfo info)
        {
            bool isAdd = (info.ID == 0) ? true : false;
            base.Save(info);

            //把模块添加到系统管理员角色里去
            if (isAdd)
            {
                string roleID = AppSetting.ModuleDefaultRoleID;
                if (roleID != null && roleID != "")
                {
                    RoleBO role = new RoleBO();
                    role.AddModuleID(int.Parse(roleID), info.ID);
                }
            }
        }

        public override ModuleInfo GetInfo(object id)
        {
            ModuleInfo info = base.GetInfo(id);

            SetDelegate(info);
            

            return info;
        }

     

        public void SetDelegate(ModuleInfo info)
        {
            info.GetUpperModuleInfo = upperID => GetInfo(info.UpperID);
            info.GetSubModuleInfos = moduleID => this.GetList(info.ID);
        }

        private void SetDelegate(IEnumerable<ModuleInfo> list)
        {
            foreach (ModuleInfo info in list)
            {
                SetDelegate(info);
            }
        }

        public void Copy(IEnumerable<int> ids)
        {
            List<ModuleInfo> list = GetList(ids);

            foreach (ModuleInfo info in list)
            {
                info.Orders = base.GetMaxOrder();
                info.ID = 0;
                base.Save(info);
            }
        }

        public void ChangeState(List<int> ids, ModuleState state)
        {
            _module.ChangeState(ids, state);
        }
    }
}
